package org.jeecg.modules.nurse_nurses.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.desensitization.enums.SensitiveEnum;
import org.jeecg.common.desensitization.util.SensitiveInfoUtil;
import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.nurse_hospital.entity.NurseHospital;
import org.jeecg.modules.nurse_hospital.service.INurseHospitalService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;

import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.nurse_nurses.entity.NurseNursesCertificate;
import org.jeecg.modules.nurse_nurses.entity.NurseNursesComment;
import org.jeecg.modules.nurse_nurses.entity.NurseNurses;
import org.jeecg.modules.nurse_nurses.service.INurseNursesService;
import org.jeecg.modules.nurse_nurses.service.INurseNursesCertificateService;
import org.jeecg.modules.nurse_nurses.service.INurseNursesCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 护工管理
 * @Author: jeecg-boot
 * @Date: 2024-06-04
 * @Version: V1.0
 */
@Api(tags = "护工管理")
@RestController
@RequestMapping("/nurse_nurses/nurseNurses")
@Slf4j
public class NurseNursesController extends JeecgController<NurseNurses, INurseNursesService> {

    @Autowired
    private INurseNursesService nurseNursesService;

    @Autowired
    private INurseNursesCertificateService nurseNursesCertificateService;

    @Autowired
    private INurseNursesCommentService nurseNursesCommentService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private INurseHospitalService iNurseHospitalService;
    @Autowired
    private ISysUserService sysUserService;

    @Value("${wechat.nurse.login-url:}")
    private String loginUrl;

    @Value("${wechat.nurse.nurese-username:}")
    private String username;

    @Value("${wechat.nurse.nurese-password:}")
    private String password;

    @AutoLog(value = "护工用户登录")
    @ApiOperation(value = "护工用户登录", notes = "护工用户登录")
    @PostMapping(value = "/login")
    public Result<?> login(@RequestBody NurseNurses nurseNurses) {

        if (!redisUtil.hasKey(nurseNurses.getOpenid())) {
            return Result.error("登录失败！未查询到护工信息");
        } else {
            redisUtil.del(nurseNurses.getOpenid());
        }

        nurseNurses = nurseNursesService.getOne(new QueryWrapper<NurseNurses>().eq("openid", nurseNurses.getOpenid()));
        if (oConvertUtils.isEmpty(nurseNurses)) {
            return Result.error("登录失败！未查询到护工信息");
        }
        JSONObject req = new JSONObject();

        req.put("username", username);
        req.put("password", password);

        JSONObject res = RestUtil.post(loginUrl, req);

        res = res.getJSONObject("result");

        if (oConvertUtils.isEmpty(res)) {
            return Result.error("登录失败！未请求到护工用户信息");
        }

        String token = res.getString("token");

        if (oConvertUtils.isEmpty(token)) {
            return Result.error("登录失败！未请求到token");
        }

        return Result.OK(token);
    }

    /**
     * 通过openid查询
     *
     * @param openid
     * @return
     */
//    @AutoLog(value = "护工用户管理-通过openid查询")
    @ApiOperation(value = "护工用户管理-通过openid查询", notes = "护工用户管理-通过openid查询")
    @GetMapping(value = "/queryByOpenId")
    public Result<?> queryByOpenId(@RequestParam(name = "openid") String openid) {

        NurseNurses nurseNurses = nurseNursesService.getOne(new QueryWrapper<NurseNurses>().
                select("id", "name", "openid", "avatar", "phone", "idcard", "sex", "status").eq("openid", openid));

        if (nurseNurses == null) {
            return Result.OK("未找到对应数据");
        }

        if (nurseNurses.getStatus().equals("0")) {
            return Result.error("该护工用户已被禁用,请联系相关工作人员！");
        }

        return Result.OK(nurseNurses);
    }

    /**
     * 获取护工加密openid
     *
     * @return
     */

    @ApiOperation(value = "护理工-获取护工加密openid", notes = "护理工-获取护工加密openid")
    @GetMapping(value = "/getEncode")
    public Result<?> getEncode(@RequestParam(name = "openid") String openid) {
        String empowerCode = SensitiveInfoUtil.getEncodeData(openid, SensitiveEnum.ENCODE);
        return Result.OK(empowerCode);
    }

    @AutoLog(value = "护理工-获取护工加密openid-返回二维码")
    @ApiOperation(value = "护理工-获取护工加密openid-返回二维码", notes = "护理工-获取护工加密openid-返回二维码")
    @GetMapping("/getQRCode/{openid}/**")
    public void getQRCode(@PathVariable String openid,
                          HttpServletResponse response) throws IOException, WriterException {
        String empowerCode = SensitiveInfoUtil.getEncodeData(openid, SensitiveEnum.ENCODE);
        int width = 200;
        int height = 200;
        String format = "jpg";
        ServletOutputStream out = response.getOutputStream();
        Map<EncodeHintType, Object> config = new HashMap<>();
        config.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        config.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        config.put(EncodeHintType.MARGIN, 0);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(empowerCode, BarcodeFormat.QR_CODE, width, height, config);
        MatrixToImageWriter.writeToStream(bitMatrix, format, out);
    }

    /**
     * 设置护工解密openid
     *
     * @return
     */
    @AutoLog(value = "护理工-设置护工解密openid")
    @ApiOperation(value = "护理工-设置护工解密openid", notes = "护理工-设置护工解密openid")
    @GetMapping(value = "/setOpenid")
    public Result<?> setOpenid(@RequestParam(name = "encode") String encode,
                               @RequestParam(name = "id") String id) {
        String openid = SensitiveInfoUtil.getDecodeData(encode);
        NurseNurses nurseNurses = nurseNursesService.getById(id);
        nurseNurses.setOpenid(openid);
        nurseNursesService.updateById(nurseNurses);
        return Result.OK("设置成功！");
    }

    /**
     * 分页微信显示列表查询
     *
     * @return
     */

    @ApiOperation(value = "护理工-分页微信显示列表查询", notes = "护理工-分页微信显示列表查询")
    @GetMapping(value = "/listByWeChat")
    public Result<?> listByWeChat(@RequestParam(name = "hospital") String hospital) {
        List<NurseNurses> nurseNurses = nurseNursesService.listByWeChat(hospital);
        //        List<NurseNurses> nurseNurses =
//                nurseNursesService.list(
//                        new QueryWrapper<NurseNurses>().
//                                select("id", "name", "avatar", "rate", "server_number", "sex", "life").
//                                eq("is_show", "1").
//                                eq("status", "1").
//                                exists("select 1 where (" + hospital + " <> '1' and nurse_nurses.hospital like '%" + hospital + "%') "));
        return Result.OK(nurseNurses);
    }


    /**
     * 微信查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "护理工管理-微信查询")
    @ApiOperation(value = "护理工管理-微信查询", notes = "护理工管理-微信查询")
    @GetMapping(value = "/queryById")
    public Result<NurseNurses> queryById(@RequestParam(name = "id", required = true) String id) {
        NurseNurses nurseNurses = nurseNursesService.getOne(new QueryWrapper<NurseNurses>().eq("id", id));
        if (nurseNurses == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(nurseNurses);

    }


    /*---------------------------------主表处理-begin-------------------------------------*/

    /**
     * 分页列表查询
     *
     * @param nurseNurses
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "护工管理-分页列表查询")
    @ApiOperation(value = "护工管理-分页列表查询", notes = "护工管理-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent = "nurse/nurse_nurses/NurseNursesList")
    public Result<IPage<NurseNurses>> queryPageList(NurseNurses nurseNurses,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "1000") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<NurseNurses> queryWrapper = QueryGenerator.initQueryWrapper(nurseNurses, req.getParameterMap());
        Page<NurseNurses> page = new Page<NurseNurses>(pageNo, pageSize);
        IPage<NurseNurses> pageList = nurseNursesService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    //@AutoLog(value = "护工管理-分页列表查询")
    @ApiOperation(value = "护工管理-分页列表查询", notes = "护工管理-分页列表查询")
    @GetMapping(value = "/listAll")
    public Result<IPage<NurseNurses>> queryPageListAll(NurseNurses nurseNurses,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "1000") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<NurseNurses> queryWrapper = QueryGenerator.initQueryWrapper(nurseNurses, req.getParameterMap());
        Page<NurseNurses> page = new Page<NurseNurses>(pageNo, pageSize);
        SysUser sysuser= sysUserService.getLoginUser();
        List<NurseHospital> list = iNurseHospitalService.list(new LambdaQueryWrapper<NurseHospital>().eq(NurseHospital::getSysOrgCode,sysuser.getOrgCode()));
        if(!list.isEmpty()){
            queryWrapper.eq("hospital",list.get(0).getId());
        }
        IPage<NurseNurses> pageList = nurseNursesService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param nurseNurses
     * @return
     */
    @AutoLog(value = "护工管理-添加")
    @ApiOperation(value = "护工管理-添加", notes = "护工管理-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody NurseNurses nurseNurses) {
        nurseNurses.setSysOrgCode((String) iNurseHospitalService.selectSysOrgCodeById(nurseNurses.getHospital()).get("sys_org_code"));
        nurseNursesService.save(nurseNurses);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param nurseNurses
     * @return
     */
    @AutoLog(value = "护工管理-编辑")
    @ApiOperation(value = "护工管理-编辑", notes = "护工管理-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody NurseNurses nurseNurses) {
        nurseNursesService.updateById(nurseNurses);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "护工管理-通过id删除")
    @ApiOperation(value = "护工管理-通过id删除", notes = "护工管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        nurseNursesService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "护工管理-批量删除")
    @ApiOperation(value = "护工管理-批量删除", notes = "护工管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.nurseNursesService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 导出
     *
     * @return
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, NurseNurses nurseNurses) {
        return super.exportXls(request, nurseNurses, NurseNurses.class, "护工管理");
    }

    /**
     * 导入
     *
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, NurseNurses.class);
    }
    /*---------------------------------主表处理-end-------------------------------------*/


    /*--------------------------------子表处理-护工证书-begin----------------------------------------------*/

    /**
     * 通过主表ID查询
     *
     * @return
     */
    //@AutoLog(value = "护工证书-通过主表ID查询")
    @ApiOperation(value = "护工证书-通过主表ID查询", notes = "护工证书-通过主表ID查询")
    @GetMapping(value = "/listNurseNursesCertificateByMainId")
    public Result<IPage<NurseNursesCertificate>> listNurseNursesCertificateByMainId(NurseNursesCertificate nurseNursesCertificate,
                                                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                                    HttpServletRequest req) {
        QueryWrapper<NurseNursesCertificate> queryWrapper = QueryGenerator.initQueryWrapper(nurseNursesCertificate, req.getParameterMap());
        Page<NurseNursesCertificate> page = new Page<NurseNursesCertificate>(pageNo, pageSize);
        IPage<NurseNursesCertificate> pageList = nurseNursesCertificateService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param nurseNursesCertificate
     * @return
     */
    @AutoLog(value = "护工证书-添加")
    @ApiOperation(value = "护工证书-添加", notes = "护工证书-添加")
    @PostMapping(value = "/addNurseNursesCertificate")
    public Result<String> addNurseNursesCertificate(@RequestBody NurseNursesCertificate nurseNursesCertificate) {
        nurseNursesCertificateService.save(nurseNursesCertificate);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param nurseNursesCertificate
     * @return
     */
    @AutoLog(value = "护工证书-编辑")
    @ApiOperation(value = "护工证书-编辑", notes = "护工证书-编辑")
    @RequestMapping(value = "/editNurseNursesCertificate", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> editNurseNursesCertificate(@RequestBody NurseNursesCertificate nurseNursesCertificate) {
        nurseNursesCertificateService.updateById(nurseNursesCertificate);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "护工证书-通过id删除")
    @ApiOperation(value = "护工证书-通过id删除", notes = "护工证书-通过id删除")
    @DeleteMapping(value = "/deleteNurseNursesCertificate")
    public Result<String> deleteNurseNursesCertificate(@RequestParam(name = "id", required = true) String id) {
        nurseNursesCertificateService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "护工证书-批量删除")
    @ApiOperation(value = "护工证书-批量删除", notes = "护工证书-批量删除")
    @DeleteMapping(value = "/deleteBatchNurseNursesCertificate")
    public Result<String> deleteBatchNurseNursesCertificate(@RequestParam(name = "ids", required = true) String ids) {
        this.nurseNursesCertificateService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 导出
     *
     * @return
     */
    @RequestMapping(value = "/exportNurseNursesCertificate")
    public ModelAndView exportNurseNursesCertificate(HttpServletRequest request, NurseNursesCertificate nurseNursesCertificate) {
        // Step.1 组装查询条件
        QueryWrapper<NurseNursesCertificate> queryWrapper = QueryGenerator.initQueryWrapper(nurseNursesCertificate, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        // Step.2 获取导出数据
        List<NurseNursesCertificate> pageList = nurseNursesCertificateService.list(queryWrapper);
        List<NurseNursesCertificate> exportList = null;

        // 过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
        } else {
            exportList = pageList;
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, "护工证书");
        mv.addObject(NormalExcelConstants.CLASS, NurseNursesCertificate.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("护工证书报表", "导出人:" + sysUser.getRealname(), "护工证书"));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

    /**
     * 导入
     *
     * @return
     */
    @RequestMapping(value = "/importNurseNursesCertificate/{mainId}")
    public Result<?> importNurseNursesCertificate(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<NurseNursesCertificate> list = ExcelImportUtil.importExcel(file.getInputStream(), NurseNursesCertificate.class, params);
                for (NurseNursesCertificate temp : list) {
                    temp.setNursesId(mainId);
                }
                long start = System.currentTimeMillis();
                nurseNursesCertificateService.saveBatch(list);
                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
                return Result.OK("文件导入成功！数据行数：" + list.size());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-护工证书-end----------------------------------------------*/

    /*--------------------------------子表处理-护工评论-begin----------------------------------------------*/

    /**
     * 通过主表ID查询
     *
     * @return
     */
    //@AutoLog(value = "护工评论-通过主表ID查询")
    @ApiOperation(value = "护工评论-通过主表ID查询", notes = "护工评论-通过主表ID查询")
    @GetMapping(value = "/listNurseNursesCommentByMainId")
    public Result<IPage<NurseNursesComment>> listNurseNursesCommentByMainId(NurseNursesComment nurseNursesComment,
                                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                            HttpServletRequest req) {
        QueryWrapper<NurseNursesComment> queryWrapper = QueryGenerator.initQueryWrapper(nurseNursesComment, req.getParameterMap());
        Page<NurseNursesComment> page = new Page<NurseNursesComment>(pageNo, pageSize);
        IPage<NurseNursesComment> pageList = nurseNursesCommentService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param nurseNursesComment
     * @return
     */
    @AutoLog(value = "护工评论-添加")
    @ApiOperation(value = "护工评论-添加", notes = "护工评论-添加")
    @PostMapping(value = "/addNurseNursesComment")
    public Result<String> addNurseNursesComment(@RequestBody NurseNursesComment nurseNursesComment) {
        nurseNursesCommentService.save(nurseNursesComment);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param nurseNursesComment
     * @return
     */
    @AutoLog(value = "护工评论-编辑")
    @ApiOperation(value = "护工评论-编辑", notes = "护工评论-编辑")
    @RequestMapping(value = "/editNurseNursesComment", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> editNurseNursesComment(@RequestBody NurseNursesComment nurseNursesComment) {
        nurseNursesCommentService.updateById(nurseNursesComment);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "护工评论-通过id删除")
    @ApiOperation(value = "护工评论-通过id删除", notes = "护工评论-通过id删除")
    @DeleteMapping(value = "/deleteNurseNursesComment")
    public Result<String> deleteNurseNursesComment(@RequestParam(name = "id", required = true) String id) {
        nurseNursesCommentService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "护工评论-批量删除")
    @ApiOperation(value = "护工评论-批量删除", notes = "护工评论-批量删除")
    @DeleteMapping(value = "/deleteBatchNurseNursesComment")
    public Result<String> deleteBatchNurseNursesComment(@RequestParam(name = "ids", required = true) String ids) {
        this.nurseNursesCommentService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 导出
     *
     * @return
     */
    @RequestMapping(value = "/exportNurseNursesComment")
    public ModelAndView exportNurseNursesComment(HttpServletRequest request, NurseNursesComment nurseNursesComment) {
        // Step.1 组装查询条件
        QueryWrapper<NurseNursesComment> queryWrapper = QueryGenerator.initQueryWrapper(nurseNursesComment, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        // Step.2 获取导出数据
        List<NurseNursesComment> pageList = nurseNursesCommentService.list(queryWrapper);
        List<NurseNursesComment> exportList = null;

        // 过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
        } else {
            exportList = pageList;
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, "护工评论");
        mv.addObject(NormalExcelConstants.CLASS, NurseNursesComment.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("护工评论报表", "导出人:" + sysUser.getRealname(), "护工评论"));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

    /**
     * 导入
     *
     * @return
     */
    @RequestMapping(value = "/importNurseNursesComment/{mainId}")
    public Result<?> importNurseNursesComment(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<NurseNursesComment> list = ExcelImportUtil.importExcel(file.getInputStream(), NurseNursesComment.class, params);
                for (NurseNursesComment temp : list) {
                    temp.setNursesId(mainId);
                }
                long start = System.currentTimeMillis();
                nurseNursesCommentService.saveBatch(list);
                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
                return Result.OK("文件导入成功！数据行数：" + list.size());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-护工评论-end----------------------------------------------*/


}
